package com.changgou.business.listener;

import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/*监听广告更新消息*/
@Component
public class AdListener {

    @RabbitListener(queues = "ad_update_queue")
    public void receiveMessage(String message){
        System.out.println("接收到的消息是"+message);

        //发起远程调用nginx
        OkHttpClient okHttpClient=new OkHttpClient();
        //请求的路径是nginx中对于redis刷新的那个lua脚本(之前编写的ad_update的那个脚本)也就是/usr/local/openresty/nginx/conf/nginx.conf中添加的请求路径,nginx是openrestry中的自动安装的那个nginx，记得开启
        String url="http://192.168.23.129/ad_update?position="+message;
        Request request=new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //请求成功
                System.out.println("请求成功："+response.message());
            }
        });

    }
}
